#!/bin/bash
# Tests named hierarchies, mouted several times
# - cgsnapshot
# - cgconfigparser
# - lssubsys

. `dirname $0`/../testenv.sh

# 10 named hierarchies, each mounted 8 times
HIERARCHIES=10
COUNT=8

function check_config()
{
	# check basic sanity of the generated file
	CONFIG=$1

	# it has expected nr. of mount points
	if [ `grep '=' <$CONFIG | wc -l` -ne $(($COUNT*$HIERARCHIES+3)) ];then
		die "wrong number of mount points displayed in $CONFIG"
	fi
	# it has mount section
	grep "mount" >/dev/null <$CONFIG \
		|| die "cgsnapshot failed, no mount section found in $CONFIG"
	# it has every hierarchy at least once
	for i in `seq $HIERARCHIES`; do
		grep "name=$i" >/dev/null <$CONFIG \
			|| die "cgsnapshot failed, no name=$i found in $CONFIG"
	done
	# it has cpuacct,cpuset,name=cputest mount
	grep "name=cputest" >/dev/null <$CONFIG \
		|| die "cannot find name=cputest in $CONFIG"
	grep "cpuacct" >/dev/null <$CONFIG \
		|| die "cannot find cpuacct in $CONFIG"
}

for i in `seq $HIERARCHIES`; do
	for j in `seq $COUNT`; do
		mkdir -p $TMP/cgroups/named$i-$j 2>/dev/null
		mount -t cgroup -o none,name=$i named$i \
			$TMP/cgroups/named$i-$j \
			|| die "Mount $i_$j failed"
	done
done

# add one named mount point with other controllers
mkdir -p $TMP/cgroups/cpu 2>/dev/null
mount -t cgroup -o cpuset,cpuacct,name=cputest test $TMP/cgroups/cpu \
		|| die "Mount cpu failed"

# !!! lssubsys does NOT support named hierarchies now !!!
# check lssubsys
# plain lssubsys - every hierarchy only once
#$TOOLSDIR/lssubsys >$TMP/subsys1.out \
#	|| die "plain lssubsys failed"
#[ `wc -l <$TMP/subsys1.out` -eq $(( $HIERARCHIES+1 )) ] \
#	|| die "unexpected output of plain lssubsys"

# lssubsys -m - first mount point is returned
#$TOOLSDIR/lssubsys -m >$TMP/subsys2.out \
#	|| die "lssubsys -m failed"
#[ `wc -l <$TMP/subsys2.out` -eq $(( $HIERARCHIES+1 )) ] \
#	|| die "unexpected output of lssubsys -m"
#grep <$TMP/subsys2.out cgroups/cpu >/dev/null \
#	|| die "lssubsys -m does not show the first mount point for cputest"
#for i in `seq $HIERARCHIES`; do
#	grep <$TMP/subsys2.out cgroups/named$i-1 >/dev/null \
#		|| die "lssubsys -m does not show cgroups/named$i-1"
#done

# lssubsys -M - all mount points are returned
#$TOOLSDIR/lssubsys -M >$TMP/subsys3.out \
#	|| die "lssubsys -M failed"
#[ `wc -l <$TMP/subsys3.out` -eq $(( $COUNT*$HIERARCHIES+1)) ] \
#	|| die "unexpected output of lssubsys -M"
#for i in `seq $HIERARCHIES`; do
#	for j in `seq $COUNT`; do
#		grep <$TMP/subsys3.out cgroups/named$i-$j >/dev/null \
#		|| die "lssubsys -m does not show cgroups/named$i-$j"
#	done
#done

$TOOLSDIR/cgsnapshot -b $CONFIGDIR/cgsnapshot_blacklist.conf >$TMP/snapshot.conf \
	|| die "cgsnapshot failed"

check_config $TMP/snapshot.conf

$TOOLSDIR/cgclear || die "cgclear 1 failed"

# try to parse the config...
$TOOLSDIR/cgconfigparser -l $TMP/snapshot.conf \
	|| die "cgconfigparser failed to parse the snapshot"
# and make its snapshot again
$TOOLSDIR/cgsnapshot -b $CONFIGDIR/cgsnapshot_blacklist.conf >$TMP/snapshot2.conf \
	|| die "cgsnapshot2 failed"

check_config $TMP/snapshot2.conf

$TOOLSDIR/cgclear

cleanup
exit 0
